/*
 *  Copyright 2013 by Sebastian Gesemann
 *
 *  This file is part of a SipHash++, a C++ SipHash implementation
 *
 *  SipHash is a family of pseudorandom functions (a.k.a. keyed hash
 *  functions) optimized for speed on short messages. It was designed
 *  by Jean-Philippe Aumasson and Daniel J. Bernstein. Check out
 *  https://131002.net/siphash/
 *
 *  SipHash++ is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  SipHash++ is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with SipHash++.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef SG_SIPHASH_HPP_INCLUDED
#define SG_SIPHASH_HPP_INCLUDED

#include <cassert>
#include <cstdint>
#include <cstddef>

namespace siphash {


using std::uint64_t;

template<int Words>
struct array
{
	uint64_t elems[Words];

	uint64_t const& operator[](unsigned idx) const {assert(idx<Words);return elems[idx];}
	uint64_t      & operator[](unsigned idx)       {assert(idx<Words);return elems[idx];}
};

typedef array<2> key_t;
typedef array<4> state_t;


/// returns a key struct that represents the given raw key
extern key_t key(const unsigned char k[16]);

/// returns an initialized SipHash state using a zero key
extern state_t init();

/// returns an initialized SipHash state using the given key
extern state_t init(key_t const& k);

/// updates the SipHash state using the given message block and two SipHash rounds
extern void update2(state_t & v, uint64_t m);

/// finishes the SipHash computation using 4 SipHash rounds, the passed state is modified
extern uint64_t finish4(state_t & v);

/// performs all SipHash-2-4 operations at once
extern uint64_t atonce24(key_t const& k, std::size_t len, const unsigned char data[]);

/// performs all SipHash-2-4 operations at once using the zero key
inline uint64_t atonce24(std::size_t len, const unsigned char data[])
{
	key_t k00 = {{0,0}};
	return atonce24(k00,len,data);
}


/// class for streaming SipHash-2-4 computation
class stream24_t
{
public:
	/// initialization with zero key
	stream24_t();

	/// initialization with given key
	explicit stream24_t(key_t const& k);

	/// reset with zero key
	void reset();

	/// reset with given key
	void reset(key_t const& k);

	/// updates the hash using the given data
	void update(std::size_t len, const unsigned char data[]);

	/// computes the final hash value
	///
	/// the state is modified in-place and you should call this
	/// only once between initializations/resets unless you
	/// know what you are doing.
	uint64_t finish();

private:
	state_t v;
	uint64_t b; //< buffer
};


} // namespace siphash

#endif // #define SG_SIPHASH_HPP_INCLUDED
